package com.csdj.t301.cloud.uitls;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * author:cloud
 * date:2021/3/3 0003
 **/
public class HttpUtil {

    public static String doPostJson(String url, Map<String,String> headers,String params) throws IOException {
        if(headers == null){
            headers = new HashMap<>();
            headers.put("Content-Type","application/json;charset=UTF-8");
        }
        return fetch("POST",url,headers,params);
    }

    public static String doPost(String url, Map<String,String> headers,Map<String,String> params) throws IOException {
        if(headers == null){
            headers = new HashMap<>();
            headers.put("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
        }
        String body = parseParam(params);
        return fetch("POST",url,headers,body);
    }

    public static String doGet(String url,Map<String,String> headers,Map<String,String> params) throws IOException {
        if(headers == null){
            headers = new HashMap<>();
            headers.put("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
        }
        String body =  parseParam(params);
        if(body != "" && body != null){
            if(url.indexOf("?")>0){
                url = url + "&"+body;
            }else{
                url = url + "?"+body;
            }
        }
        return fetch("GET",url,headers,null);
    }


    public static String fetch(String method,String url,Map<String,String> headers,String body) throws IOException {
        URL u = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) u.openConnection();
        conn.setConnectTimeout(100000);//连接超时
        conn.setReadTimeout(300000);//读取数据超时
        if(method != null){
            conn.setRequestMethod(method);
        }
        if(headers != null){
            //添加头信息
            for(Map.Entry<String,String> entry : headers.entrySet()){
                conn.addRequestProperty(entry.getKey(), entry.getValue());
            }
        }
        if(body != null){
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            os.write(body.getBytes());
            os.flush();
            os.close();
        }
        InputStream is = conn.getInputStream();
        StringBuilder sb = new StringBuilder();
        BufferedReader buf = new BufferedReader(new InputStreamReader(is,"utf-8"));
        String line;
        while((line = buf.readLine()) != null){
            sb.append(line);
        }
        is.close();
        // handle redirects
        if (conn.getResponseCode() == 301) {
            String location = conn.getHeaderField("Location");
            return fetch(method, location, headers, body);
        }
        return sb.toString();
    }

    public static String parseParam(Map<String,String> param){
        StringBuilder sb = new StringBuilder();
        if(param != null){
            for(Map.Entry<String,String> entry: param.entrySet()){
                sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
            return sb.substring(0,sb.length()-1);
        }
        return "";
    }
}
